package com.example.finalssm.Controller;

import com.example.finalssm.mapper.CommentMapper;
import com.example.finalssm.mapper.QuestionMapper;
import com.example.finalssm.mapper.ReplyMapper;
import com.example.finalssm.utils.MybatisUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/blogger")
public class ReplyController {
    //用户回答问题
    @RequestMapping("/answer")
    @ResponseBody
    public String answerQuestion(@RequestParam("answer") String answer, @RequestParam("questionId") String questionId, HttpSession session){
        Map<String,String> map=new HashMap<>();
        map.put("userId",(String) session.getAttribute("userId"));
        map.put("replyId",questionId);
        map.put("reply",answer);
        int i = MybatisUtils.getSqlSession().getMapper(ReplyMapper.class).answer(map);
        if(i==1)
        return "回答成功";
        else return "回答失败";
    }
    //用户删除问题
    @RequestMapping("/deleteAnswer")
    @ResponseBody
    //3
    public  String deleteAnswer(@RequestParam("GPS_replyId") String id){
        int i = MybatisUtils.getSqlSession().getMapper(ReplyMapper.class).deleteAnswer(id);
        //用户删除回答之后，这个回答的所有评论都要删除才对
        int j=MybatisUtils.getSqlSession().getMapper(CommentMapper.class).deleteComment01(id);
        if(i==1)
            return "删除成功";
        else return "删除失败";
    }
    @RequestMapping("/upDateAnswer")
    @ResponseBody
    //3
    public  String upDateAnswer(@RequestParam("answer")String answer,@RequestParam("GPS_replyId") String id){
        int i = MybatisUtils.getSqlSession().getMapper(ReplyMapper.class).upDateAnswer(answer, id);
        if(i==1)
            return "修改成功";
        else return "修改失败";
    }
}
